<!DOCTYPE html>
<html>
<head>
    {include file="_Fragment/meta" /}
    <link rel="stylesheet" href="/static/pear/component/pear/css/pear.css" />
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" lay-filter="reload">
            <div class="layui-form-item">

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" name="title" placeholder="课件名称" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <select name="type">
                            <option value="">课件类型</option>
                            <option value="1">图文</option>
                            <option value="2">文档</option>
                            <option value="3">测试</option>
                            <option value="4">survey</option>
                            <option value="5">外链</option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <select name="cate_id">
                            <option value="">课件分类</option>
                            {volist name="cate" id="vo"}
                            <option value="{$vo.id}">{$vo.title}</option>
                            {/volist}
                        </select>
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <select name="label_id">
                            <option value="">课件标签</option>
                            {volist name="label" id="vo"}
                            <option value="{$vo.id}">{$vo.title}</option>
                            {/volist}
                        </select>
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <select name="open">
                            <option value="">是否公开</option>
                            <option value="1">公开</option>
                            <option value="2">非公开</option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" name="time" id="date" placeholder="创建时间" autocomplete="off" class="layui-input">
                    </div>
                </div>


                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="user-table" lay-filter="user-table"></table>
    </div>
</div>

<script type="text/html" id="user-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
</script>

<script type="text/html" id="user-bar">
    <button class="pear-btn pear-btn-sm" id="more_{{d.id}}"><i class="layui-icon layui-icon-triangle-d"></i></button>
</script>
<script type="text/html" id="table-type">
    {{#if (d.type == '1') { }}
    <span>图文</span>
    {{# }else if(d.type == '2'){ }}
    <span>文档</span>
    {{# }else if(d.type == '3'){ }}
    <span>测试</span>
    {{# }else if(d.type == '4'){ }}
    <span>survey</span>
    {{# }else if(d.type == '5'){ }}
    <span>外链</span>
    {{# } }}
</script>
<script type="text/html" id="table-open">
    {{#if (d.open == '1') { }}
    <span>公开</span>
    {{# }else if(d.open == '2'){ }}
    <span>非公开</span>
    {{# } }}
</script>

<script type="text/html" id="create-time">
    {{layui.util.toDateString(d.add_time*1000, 'yyyy-MM-dd HH:mm:ss')}}
</script>

<script type="text/html" id="table-duration">
    {{d.duration/60}}
</script>
{include file="_Fragment/javascript" /}
<script>
    layui.use(['table', 'form', 'jquery','common', 'laydate', 'dropdown'], function() {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;
        let laydate = layui.laydate;
        let dropdown = layui.dropdown;

        laydate.render({
            elem: '#date'
            ,range: true //或 range: '~' 来自定义分割字符
            ,trigger: 'click'
            ,type: 'date'
        });

        let cols = [
            [{
                title: 'ID',
                field: 'id',
                align: 'center',
                fixed: 'left',
                width:'10%',
                sort: true
            },
                {
                    title: '课件名称(CN)',
                    field: 'title',
                    align: 'center',
                    fixed: 'left',
                    width:'20%'
                },
                {
                    title: '课件名称(EN)',
                    field: 'e_title',
                    align: 'center',
                    fixed: 'left',
                    width:'20%'
                },
                {
                    title: '课件分类',
                    field: 'cate_name',
                    align: 'center',
                    width:'10%'
                },
                {
                    title: '课件标签',
                    field: 'label_name',
                    align: 'center',
                    width:'10%'
                },
                {
                    title: '课件时长(分钟)',
                    field: 'duration',
                    align: 'center',
                    templet: '#table-duration',
                    width:'10%'
                },
                {
                    title: '课件类型',
                    field: 'type',
                    align: 'center',
                    templet:'#table-type',
                    width:'10%'
                },
                {
                    title: '是否公开',
                    field: 'open',
                    templet:'#table-open',
                    align: 'center',
                    width:'10%'
                },
                {
                    title: '创建人',
                    field: 'account',
                    align: 'center',
                    width:'10%'
                },
                {
                    title: '创建时间',
                    field: 'add_time',
                    align: 'center',
                    templet: '#create-time',
                    width:'20%',
                    sort: true
                },
                {
                    title: '添加次数',
                    field: 'num_of_add',
                    align: 'center',
                    width:'10%',
                    sort: true
                },
                {
                    title: '操作',
                    toolbar: '#user-bar',
                    align: 'center',
                    width:'10%'
                }
            ]
        ]

        table.render({
            elem: '#user-table',
            url: '/Courseware/load',
            page: {layout:['count', 'prev', 'page', 'next', 'skip']},
            limit: 10,
            cols: cols,
            //skin: 'line',
            toolbar: '#user-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter',{
                title: '导出' //标题
                ,layEvent: 'export' //事件名，用于 toolbar 事件中使用
                ,icon: 'layui-icon-export' //图标类名
            }],
            done: function(res, curr, count) {
                for (var i = 0; i < res.data.length; i++) {
                    if (res.data[i].type == 5) {
                        if (res.data[i].ok == 1) {
                            var datas = [{
                                title: '修改',
                                id: res.data[i].id,
                                type: 1,
                                type1: res.data[i].type
                            }, {
                                title: '复制',
                                id: res.data[i].id,
                                type: 2
                            }, {
                                title: '删除',
                                id: res.data[i].id,
                                type: 3
                            }]
                        } else {
                            var datas = [{
                                title: '复制',
                                id: res.data[i].id,
                                type: 2
                            }]
                        }
                    } else {
                        if (res.data[i].ok == 1) {
                            var datas = [{
                                title: '修改',
                                id: res.data[i].id,
                                type: 1,
                                type1: res.data[i].type
                            }, {
                                title: '复制',
                                id: res.data[i].id,
                                type: 2
                            }, {
                                title: '删除',
                                id: res.data[i].id,
                                type: 3
                            }, {
                                title: '学习记录',
                                id: res.data[i].id,
                                type: 4
                            }]
                        } else {
                            var datas = [{
                                title: '复制',
                                id: res.data[i].id,
                                type: 2
                            }, {
                                title: '学习记录',
                                id: res.data[i].id,
                                type: 4
                            }]
                        }
                    }

                    dropdown.render({
                        elem: '#more_' + res.data[i].id,
                        data: datas,
                        id: '#more_' + res.data[i].id,
                        click: function(obj) {
                            if (obj.type == 1) {
                                if (obj.type1 == 1 || obj.type1 == 2 || obj.type1 == 5) {
                                    parent.layui.admin.addTab({$rand}, "修改课件", '/Courseware/edit?id='+obj.id);
                                } else if(obj.type1 == 3) {
                                    parent.layui.admin.addTab({$rand}, "修改课件", '/Courseware/test?id='+obj.id);
                                } else if(obj.type1 == 4) {
                                    parent.layui.admin.addTab({$rand}, "修改课件", '/Courseware/editSurvey?id='+obj.id);
                                }

                            } else if (obj.type == 2) {

                                layer.confirm('确定要复制课件吗?', {
                                    icon: 3,
                                    title: '提示'
                                }, function(index) {
                                    layer.close(index);
                                    let loading = layer.load();
                                    $.ajax({
                                        url: "/Courseware/copy",
                                        data:{id:obj.id},
                                        dataType: 'json',
                                        type: 'post',
                                        success: function(result) {
                                            layer.close(loading);
                                            if (result.status == 0) {
                                                layer.msg(result.msg, {
                                                    icon: 1,
                                                    time: 1000
                                                }, function() {
                                                    table.reload('user-table');
                                                });
                                            } else {
                                                layer.msg(result.msg, {
                                                    icon: 2,
                                                    time: 1000
                                                });
                                            }
                                        }
                                    })
                                });

                            } else if(obj.type == 3) {

                                layer.confirm('确定要删除课件吗?', {
                                    icon: 3,
                                    title: '提示'
                                }, function(index) {
                                    layer.close(index);
                                    let loading = layer.load();
                                    $.ajax({
                                        url: "/Courseware/delete",
                                        data:{id:obj.id},
                                        dataType: 'json',
                                        type: 'post',
                                        success: function(result) {
                                            layer.close(loading);
                                            if (result.status == 0) {
                                                layer.msg(result.msg, {
                                                    icon: 1,
                                                    time: 1000
                                                }, function() {
                                                    table.reload('user-table');
                                                });
                                            } else {
                                                layer.msg(result.msg, {
                                                    icon: 2,
                                                    time: 1000
                                                });
                                            }
                                        }
                                    })
                                });
                            } else if(obj.type == 4) {
                                layer.open({
                                    type: 2,
                                    title: '学习记录',
                                    shade: 0.1,
                                    area: [common.isModile()?'100%':'900px', common.isModile()?'100%':'600px'],
                                    content: '/Courseware/classesRecord?id='+obj.id
                                });
                            }
                        }
                    });
                }
            }
        });
        form.on('submit(user-query)', function(data) {
            table.reload('user-table', {
                where: data.field
                ,page: {
                    curr: 1, //重新从第 1 页开始
                    layout:['count', 'prev', 'page', 'next', 'skip']
                }
                ,limit: 10,
            })
            return false;
        });

        //监听排序事件
        table.on('sort(user-table)', function(obj){ //注：sort 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var field = form.val('reload');
            table.reload('user-table', {
                initSort: obj //记录初始排序，如果不设的话，将无法标记表头的排序状态。
                ,where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                    field: obj.field //排序字段
                    ,order: obj.type //排序方式
                    ,title: field.title
                    ,type: field.type
                    ,cate_id: field.cate_id
                    ,label_id: field.label_id
                    ,time: field.time
                    ,open: field.open
                }
            });
        });


        table.on('toolbar(user-table)', function(obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'export') {
                window.export();
            }
        });

        // 数据导出
        window.export = function() {
            var field = form.val('reload');
            layer.confirm('确定要导出课件池数据吗?', {
                icon: 3,
                title: '提示'
            }, function(index) {
                layer.close(index);
                window.open("{:url('export')}?title="+field.title+"&cate_id="+field.cate_id+"&label_id="+field.label_id+"&time="+field.time+"&type="+field.type+"&open="+field.open, "_blank");
            });
        };

        window.add = function() {
            parent.layui.admin.addTab({$rand1}, "新增课件", '/Courseware/add')
        }

        window.refresh = function(param) {
            table.reload('user-table');
        }
    })
</script>
</body>
</html>
